package file.listener;

import file.FileFrame;
import file.handle.FileHandle;
import file.handle.FilePannel;
import file.model.FileModel;
import file.untils.StringUntils;

import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;

/**
 * @Author: zou
 * @email: 704572528@qq.com
 * @Date: 2020/10/31 12:19
 */
public class ChangeNameBtnListener extends FileFrameAbstract implements MouseListener {

    public ChangeNameBtnListener(FileFrame fileFrame) {
        super(fileFrame);
    }
    private FileHandle fileHandel = new file.handle.impl.FileHandleImpl();
    private FilePannel filePannel = new file.handle.impl.FilePannelImpl(fileFrame);
    @Override
    public void mouseClicked(MouseEvent e) {
        JTextArea cl = fileFrame.getChangeLable();
        String text = cl.getText();
        if (!text.isEmpty()) {
            FileModel or = new FileModel();
            or.setPath(fileFrame.getHiddleLable().getText());
            FileModel newName = new FileModel();
            newName.setPath(StringUntils.exchangeName(fileFrame.getHiddleLable().getText(), text));
            fileHandel.changeFileName(or, newName);
            updateUi();
            fileFrame.getDialog1().dispose();
        }
    }

    private void updateUi() {
        File file = new File(getPathLable());
        JPanel filePanel = this.fileFrame.getFilePanel();
        // 清除布局
        filePannel.removeFile(filePanel);
        // 文件处理
        filePannel.fileHandel(file,filePanel);
        // 更新Ui
        filePannel.updateUI(filePanel);
    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}
